package com.codingfuture.glacierhardware.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.constant.ResultConstant;
import com.codingfuture.glaciercommon.entity.Meter;
import com.codingfuture.glaciercommon.util.EntityConverter;
import com.codingfuture.glacierhardware.dto.MeterAddDTO;
import com.codingfuture.glacierhardware.dto.MeterUpdateDTO;
import com.codingfuture.glacierhardware.entity.Company;
import com.codingfuture.glacierhardware.entity.Host;
import com.codingfuture.glacierhardware.entity.WareHouse;
import com.codingfuture.glacierhardware.mapper.CompanyMapper;
import com.codingfuture.glacierhardware.mapper.HostMapper;
import com.codingfuture.glacierhardware.mapper.MeterMapper;
import com.codingfuture.glacierhardware.mapper.WareHouseMapper;
import com.codingfuture.glacierhardware.service.MeterService;
import com.codingfuture.glacierhardware.vo.CompanyVO;
import com.codingfuture.glacierhardware.vo.HostVO;
import com.codingfuture.glacierhardware.vo.WareHouseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: Sun
 * @DateTime: 2025/3/31 16:17
 * @Description:
 **/
@Service
@Slf4j
public class MeterServiceImpl implements MeterService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private WareHouseMapper wareHouseMapper;
    @Autowired
    private HostMapper hostMapper;
    @Autowired
    private MeterMapper meterMapper;

    @Cacheable(value = "meter", sync = true)
    @Override
    public Page<Meter> findByPage(String companyName, Page<Meter> page) {
//        判断字符串是不是空
//       boolean blank = warehouseName.isEmpty()//判断点前面的字符串是不是空串   什么不写才算空字符串
//        boolean blank = warehouseName.isBlank();//判断点前面的字符串是不是空串，输出空格也算空串
//        wrapper.like(!blank, "warehouse_name", warehouseName);
        //利用blank在接口文档中会报空指针异常的错误
//        http://localhost:8081/meter/listByPage?pageNum=1&pageSize=10&warehouseName=


//        boolean empty = StringUtils.isEmpty(warehouseName);//判断空号里面的是不是空串
//        wrapper.like(!empty, "warehouse_name", warehouseName);
        QueryWrapper<Meter> wrapper = new QueryWrapper<>();
        wrapper.like(!(companyName == null || "".equals(companyName)), "warehouse_name", companyName);
        return meterMapper.selectPage(page, wrapper);
    }

    @CacheEvict(value = "meter", allEntries = true)
    @Override
    public boolean addInfo(MeterAddDTO meterAddDTO) {
        if (meterAddDTO.getMaxTem() <= meterAddDTO.getMinTem()) {
            return false;
        }
        if (meterAddDTO.getMaxHum() <= meterAddDTO.getMinHum()) {
            return false;
        }
        return meterMapper.addInfo(meterAddDTO);
    }
    @CacheEvict(value = "meter", allEntries = true)
    @Override
    public boolean updateInfo(MeterUpdateDTO meterUpdateDTO) {

        return meterMapper.updateInfo(meterUpdateDTO);
    }
    @CacheEvict(value = "meter", allEntries = true)
    @Override
    public boolean delById(Long id) {

        return meterMapper.deleteById(id) > ResultConstant.ZERO;
    }

    @Cacheable(value = "company", sync = true)
    @Override
    public List<CompanyVO> getCompany() {
        List<Company> companies = companyMapper.selectList(null);
        List<CompanyVO> companyVOS = new ArrayList<>();
        for (Company company : companies) {
            CompanyVO companyVO = EntityConverter.entity2DTO(company, CompanyVO.class);
            companyVOS.add(companyVO);
        }
        return companyVOS;
    }

    @Cacheable(value = "warehouse", sync = true)
    @Override
    public List<WareHouseVO> getWareHouse(Long id) {
        QueryWrapper<WareHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id", id).orderByAsc("id");
        List<WareHouse> wareHouseList = wareHouseMapper.selectList(wrapper);
        List<WareHouseVO> wareHouseVOList = new ArrayList<>();
        for (WareHouse wareHouse : wareHouseList) {
            WareHouseVO wareHouseVO = EntityConverter.entity2DTO(wareHouse, WareHouseVO.class);
            wareHouseVOList.add(wareHouseVO);
        }
        return wareHouseVOList;
    }

    @Cacheable(value = "meter", sync = true)
    @Override
    public Meter findById(Long id) {
        return meterMapper.selectById(id);
    }

    @Cacheable(value = "host", sync = true)
    @Override
    public List<HostVO> getHost(Long id) {
        QueryWrapper<Host> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id", id);
        List<Host> hostList = hostMapper.selectList(wrapper);
        List<HostVO> HostVOList = new ArrayList<>();
        for (Host host : hostList) {
            HostVO hostVO = EntityConverter.entity2DTO(host, HostVO.class);
            HostVOList.add(hostVO);
        }
        return HostVOList;
    }

    @Cacheable(value = "meter", sync = true)
    @Override
    public List<Meter> getMeterExcel() {
        List<Meter> meterList = meterMapper.selectList(null);
        return meterList;
    }
}
